package com.shen.module_room

import androidx.room.*
import com.shen.library_base.BaseApp
import com.shen.module_room.bean.ChatRecordBean
import com.shen.module_room.bean.UserInfoBean
import com.shen.module_room.dao.ChatRecordDao
import com.shen.module_room.dao.UserInfoDao
import com.shen.module_room.migration.MigrationObj.MIGRATION_1_2
import com.shen.module_room.migration.MigrationObj.MIGRATION_2_3
import com.shen.module_room.migration.MigrationObj.MIGRATION_3_4

/**
 * author:  shen
 * date:    2020/3/12
 *
 */
//@Database(entities = [UserInfoBean::class], version = 3)
@Database(entities = [UserInfoBean::class, ChatRecordBean::class], version = 4)
abstract class AppDataBase : RoomDatabase() {

    abstract fun getUserInfoDao(): UserInfoDao
    abstract fun getChatRecordDao(): ChatRecordDao

    companion object {
        const val DATABASE_NAME = "SHEN.db"

        @Volatile
        private var instance: AppDataBase? = null
        fun getDBInstance(): AppDataBase {
            if (instance == null) {
                synchronized(AppDataBase::class) {
                    if (instance == null) {
                        instance = Room.databaseBuilder(BaseApp.instance, AppDataBase::class.java, DATABASE_NAME)
                            .allowMainThreadQueries()   //允许在主线程中查询
                            // .addMigrations(MIGRATION_1_2)
                            // .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
                            .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
                            .build()
                    }
                }
            }
            return instance!!
        }
    }
}